--used globlas 


dofile('globalfunc.lua')

Gameelement = {
	
	name = 'gameelement',
	sprite = "resources/default.png",
	image = {
		'resources/default.png',
		states = { dead = {"resources/gravestone.png"}} 
	},
	
	x = 0, -- don't use this values any more, use center insteadmove
	y = 0,
	center ={0,0}, --center of the picture
	width = 0, 
	height = 0,
	collisionCenter ={0,0},-- devation of the collision center from the pictur center
	collisionWidth = 0,
	collisionHeight = 0,
	offsetWidth = 0.8,
	offsetHeight = 0.8,
	xBox = 1,
	yBox = 1,
	passable = true,
	moving = true,
	velocity = {0,0},
	currentImage = 2,
	team = 0,
	direction = 1,
  blood_image = "resources/blood.png", 
	actions = { --default actions, if not in state
		resurrect = function (t) t:changeState(t.initial_state) end,
		getHit = function (t) print("autsch!") end
	},
	coolDown = {},--fireball = newCountdown(0)},
	
	stateList = {
		default  = {
			name = "default",
			actions = {}
		},

		destroy = {
			name = "destroy",
			actions = {}	
		},

		dead = {
			name = "dead",
			actions = {}
		}
	}
}

function Gameelement:new(o) 
	o = o or {}
	o.stateList = o.stateList or {}
	o.image = o.image or {}
	o.coolDown = o.coolDown or {}
	o.offsetCenter = o.offsetCenter or {}
	o.center = o.center or {}
	o.velocity = o.velocity or {}
	
	setmetatable(o, self)
	self.__index = self
  setmetatable(o.stateList, self.stateList)
	self.stateList.__index = self.stateList
	setmetatable(o.image, self.image)
	self.image.__index = self.image
	setmetatable(o.coolDown, self.coolDown)
	self.coolDown.__index = self.coolDown
	setmetatable(o.collisionCenter, self.collisionCenter)
	self.collisionCenter.__index = self.collisionCenter
	setmetatable(o.center, self.center)
	self.center.__index = self.center
	setmetatable(o.velocity, self.velocity)
	self.velocity.__index = self.velocity
	return o 
end

function Gameelement:setBox(_x,_y) 
	self.xBox = _x
	self.yBox = _y
end

function Gameelement:move(_dx, _dy) -- move an Gameelement
	self.center[1] = self.center[1] + _dx
	self.center[2] = self.center[2] + _dy

	--remove when center is used for paint and physic
	self.x = self.x + _dx
	self.y = self.y + _dy 
end

function Gameelement:dealDamage(target, amount, pCauser)
	if pCauser then 
		causer = pCauser
	else
		causer = self
	end
	
	if self.team ~= target.team and target.team ~= 0  then
		-- NOTE: getDamage function only for permanentGameelements
		target:getDamage(amount,causer)
	end
end

-- -- -- -- -- -- -- -
-- -- OVERHEAD TEXT --
-- -- -- -- -- -- -- -

function Gameelement:newOht(pText, pSpeed,pTime)
	if
		type(pText) == number then text = tostring(pText) 
	elseif pText == nil then
		text = " "
	end
	o = {
		x = self.x,
		y = self.y - 10,
		speed = pSpeed,
		countDown = newCountdown(pTime),
		text = pText,
	}
	table.insert(ohtList, o)
end
	

-- -- -- -- -- -- -- --
-- -- STATUSCHECK -- --   TODO: remove this to the right place
-- -- -- -- -- -- -- --
function Gameelement:statusCheck()

end

function Gameelement:runState()
	for i,v in	pairs (self.currentState.actions) do 
		v(self)
	end
end	

function Gameelement:changeState(newState)
	if newState then self.currentState = newState end
end

Gameelement.currentState = Gameelement.stateList.default

dofile('permanentGameelement.lua')
dofile("item.lua")
dofile('projectile.lua')




--for beat

State = {}
function State:addAction(actionname, action, ttl)
	ttl = ttl or 0
	-- print(type(action))
  -- action = function () print("hey!") end
  if type(action) == "function" and type(ttl) == "number" then
		self.actions[actionname] = {action = action, ttl = ttl or 0}
  else
		-- TODO: engine print
		--print("wrong input action: actionname, action, ttl (got " .. type(action) .. " " ..type(ttl) .. ")")
	end
end
State:addAction{"test", moveRandomly, ttl = 0}


function Gameelement:runAction(actionname, ...)
  --print("x " .. self.x .." y " .. self.y)
  --print("currentState " .. self.currentState.name .." y " .. self.y)
	if self.currentState.actions[actionname] then
		self.currentState.actions[actionname].action(...)
  elseif self.actions[actionname] then
		self.actions[actionname].action(...)
  else
		--TODO: engine_print o.ä.
		if type(actionname) ~= "string" then
    	print("wrong input argument. i need a string")
		else
			print("there's no action called "..actionname)
			for k,v in pairs(self.currentState.actions) do
				print(k)
				print(v)
			end
		end
	end
end
